package cn.rongdao.jrkabao.ui.IdentificationVerificate.uicomponent;

import android.content.Context;
import android.support.annotation.Nullable;
import android.text.Editable;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.SpannedString;
import android.text.TextWatcher;
import android.text.style.AbsoluteSizeSpan;
import android.util.AttributeSet;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.mifengkong.frtools.util.FRString;

import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;

import cn.rongdao.jrkabao.R;

/**
 * Created by jack on 2017/10/11
 */

public class IdentifyVerifyInputUIComponent extends LinearLayout {
    public IdentifyVerifyInputUIComponent(Context context) {
        super(context);
    }

    public IdentifyVerifyInputUIComponent(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public IdentifyVerifyInputUIComponent(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    private TextView tvName;
    private EditText etNumber;

    private OnNumberTextChangerListener onNumberTextChangerListener;

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        tvName = findViewById(R.id.livi_tv_name);
        etNumber = findViewById(R.id.livi_et_number);
    }

    public void setNameEditVisible(boolean isVisible) {
        etNumber.setEnabled(isVisible);
    }

    public String getEditTextContent() {
        return etNumber.getText().toString().trim();
    }

    public void setNumberStyle() {
        tvName.setText("身份证号");
        setEditTextHint("请输入身份证号");
        setTextChangeListener(false);
    }

    private void setTextChangeListener(final boolean isName) {
        etNumber.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (!isName) {
                    String editable = etNumber.getText().toString();
                    String str = stringFilter(editable);
                    if (!editable.equals(str)) {
                        etNumber.setText(str);
                        //设置新的光标所在位置
                        etNumber.setSelection(str.length());
                    }
                }
            }

            @Override
            public void afterTextChanged(Editable s) {
                if (null != onNumberTextChangerListener) {
                    onNumberTextChangerListener.onChangeListener(!FRString.isEmpty(s.toString()));
                }
            }
        });
    }

    public void setEditTextContent(String content) {
        etNumber.setText(FRString.valueOf(content));
    }

    private String stringFilter(String str) throws PatternSyntaxException {
        // 只允许字母、数字和汉字
        String regEx = "[^xX0-9]";
        Pattern p = Pattern.compile(regEx);
        Matcher m = p.matcher(str);
        return m.replaceAll("").trim();
    }

    public void setNameStyle() {
        setTextChangeListener(true);
        tvName.setText("姓名");
        setEditTextHint("请输入姓名");
    }

    private void setEditTextHint(String hint){
        SpannableString ss = new SpannableString(hint);
        AbsoluteSizeSpan ass = new AbsoluteSizeSpan(13, true);
        ss.setSpan(ass, 0, ss.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        etNumber.setHint(new SpannedString(ss));
    }

    public void setOnNumberTextChangerListener(OnNumberTextChangerListener onNumberTextChangerListener) {
        this.onNumberTextChangerListener = onNumberTextChangerListener;
    }

    public interface OnNumberTextChangerListener {
        void onChangeListener(boolean isChange);
    }
}